﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Quartz;
using Quartz.Impl;
using ServiceJob;
using System.Data;
using QuartzScheduler.ExpandClass;

namespace QuartzScheduler.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult JobSet()
        {
            ViewData["jobID"] = Request["jobID"];
            ViewData["jobClass"] = Request["jobClass"];
            return View();
        }
        public ActionResult showJobTree()
        {
            string sql = "select * from T_Job";
            SqlServerClass ssc = new SqlServerClass();
            DataTable dt = ssc.MySqlDataTable(sql);
            List<IDictionary<string, object>> rows = new List<IDictionary<string, object>>();
            foreach (DataRow r in dt.Rows)
            {
                Dictionary<string, object> Rattributes = new Dictionary<string, object>();
                Rattributes.Add("jobClass", r["jobClass"].ToString());
                Dictionary<string, object> row = new Dictionary<string, object>();
                row.Add("id", r["jobID"].ToString());
                row.Add("text", r["jobName"].ToString());
                row.Add("attributes", Rattributes);
                row.Add("state", "open");
                rows.Add(row);
            }
            return this.Json(rows);
        }
        public ActionResult showWorkJob()
        {
            string sql = "select * from T_RunJob";
            SqlServerClass ssc = new SqlServerClass();
            DataTable dt = ssc.MySqlDataTable(sql);
            List<IDictionary<string, string>> dictList = new List<IDictionary<string, string>>();
            foreach (DataRow r in dt.Rows)
            {
                Dictionary<string, string> dict = new Dictionary<string, string>();
                dict.Add("jobName", r["jobName"].ToString());
                dict.Add("beginDateTime", r["beginDateTime"].ToString());
                dict.Add("endDateTime", r["endDateTime"].ToString());
                dict.Add("descriptionInfo", r["descriptionInfo"].ToString());
                dict.Add("cronExpression", r["cronExpression"].ToString());
                dict.Add("triggertime", r["triggertime"].ToString());
                dictList.Add(dict);
            }
            Dictionary<string, object> returnDict = new Dictionary<string, object>();
            returnDict.Add("pageList", null);
            returnDict.Add("pageSize", 10);
            returnDict.Add("total", dictList.Count);
            returnDict.Add("rows", dictList);
            return this.Json(returnDict);
        }

        public ActionResult VJobName(string jobName)
        {
            string sql = "select * from T_RunJob where jobName='"+jobName+"'";
            SqlServerClass ssc = new SqlServerClass();
            DataTable dt = ssc.MySqlDataTable(sql);
            if (dt.Rows.Count > 0)
            {
                return this.Json(0);
            }
            else
            {
                return this.Json(1);
            }
        }
        public ActionResult doAdd()
        {
            try
            {
                JobManager.AddJob(Request);
                return this.Json(1);
            }
            catch
            {
                return this.Json(0);
            }
            
        }
        public ActionResult EndScheduler(string jobName)
        {
            try
            {
                IScheduler scheduler = SchedulerManager.GetScheduler();
                if (scheduler.IsStarted)
                {
                    
                    scheduler.DeleteJob(jobName,JobClassReturn.DEFAULT_GROUP);
                    //SchedulerManager.GetScheduler().Shutdown(false);
                    string sql = "delete from T_RunJob where jobName='" + jobName + "'";
                    SqlServerClass ssc = new SqlServerClass();
                    DataTable dt = ssc.MySqlDataTable(sql);
                }
                return this.Json(1);
            }
            catch (Exception ee)
            {
                return this.Json(ee.Message);
            }
        }
    }
}
